ElementsLevel.prototype = new Sprite();

function ElementsLevel(type){
    this.name = "levelElements";
    Sprite.apply(this);
    Level.apply(this);
    this.requiredTime = 50;
    this.type = type;
    this.manual = new Manual (Game.picturesPath + "levels/"+this.name + "/");
    
    this.mamOznaceneObr = false;
    this.oznacenyObr = null;
    this.mamOznaceneAt = false;
    this.oznacenyAt = null;
    this.obrpolicka;
    this.atpolicka;
    this.counter = 20;
                
    // methods
    this.init = init;
    this.overDvojicu = overDvojicu;
    
    function init(){
        this.atpolicka = new AtomyPolicka(25,30);
        this.atpolicka.init();
        this.atpolicka.miesanie();
        this.addChild(this.atpolicka);
        this.graphics.lineStyle(5,black);
        this.graphics.moveTo(MainContainerWidth/2, 30);
        this.graphics.lineTo(MainContainerWidth/2, MainContainerHeight -30);
        this.obrpolicka = new obrPolicka(MainContainerWidth/2 +20, 30);
        this.obrpolicka.init();
        this.obrpolicka.miesanie();
        this.addChild(this.obrpolicka);
        this.addChild (this.manual);
    }
}
                 
function overDvojicu (target){
    console.debug(target.parent);// target.parent);
    if (this.mamOznaceneAt && this.mamOznaceneObr){
        if (this.oznacenyObr.poradie == this.oznacenyAt.poradie){
            //console.debug(obrpolicka);
            this.obrpolicka.removeChild(this.oznacenyObr);
            this.atpolicka.removeChild(this.oznacenyAt);
            this.mamOznaceneObr = false;
            this.oznacenyObr = null;
            this.mamOznaceneAt = false;
            this.oznacenyObr = null;
            //console.debug("hura mas dvojicu");
            this.counter--;
        } else{
            console.debug("smutne");
            console.debug("obr "+ this.oznacenyObr.poradie);
            console.debug("at "+ this.oznacenyAt.poradie);
        }
    }
    if (this.counter == 0){
        target.parent.levelSolved();
        target.parent.timer.TimerStop();
    }    
}
            
            
function obrPolicka(x,y){
    Sprite.apply(this);
    this.x = x;
    this.y = y;
    this.obrpolArray = Array (4);
               
    this.init = init;
    function init (){
        for(var i = 0; i < 4; i++){
            var array = new Array(5);  
            for (var j = 0; j < 5; j++){
                var obr = new Obrazok(i, j);
                array[j] = obr;
                this.addChild(array[j]);
            }
            this.obrpolArray[i] = array;
        }
    }
                
    this.vloz = vloz;
    function vloz(obb,i,j){  
        obb.setIJ(i, j);
        obb.oznacene = false;
        this.obrpolArray[i][j] = obb; 
    }
                
    this.miesanie = miesanie; 
    function miesanie(){
        for(var i = 0; i < 4; i++){  
            for (var j = 0; j < 5; j++){
                var randomI = Math.floor((Math.random()*4));
                var randomJ = Math.floor((Math.random()*5));
                var pom =  this.obrpolArray[i][j];
                this.vloz(this.obrpolArray[randomI][randomJ],i,j);
                this.vloz(pom,randomI,randomJ);
            }
        }
    }
}
obrPolicka.prototype = new Sprite ();
            
function Obrazok (i,j){
    Sprite.apply(this);
    this.poradie = i*5+j;
    this.oznacene = false;
    this.buttonMode = true;
               
    const IMAGE_URL= Game.picturesPath+"levels/levelElements/"+"ob/"+this.poradie+".jpg";
    this.bitmap = Game._game.bitmaps[IMAGE_URL];
    this.bitmap.x = 0;
    this.bitmap.y = 0;
    this.bitmap.alpha = 1;        
    this.buttonMode = true;
    this.mouseChildren=false;
    this.addChild(this.bitmap); 
                
                
    this.setIJ = function(i,j){
        this.x = i*85;
        this.y = j*85+10;
        this.i = i;
        this.j = j;
    }
    this.setIJ(i,j);
        
    this.down = down;
    function down(e){
        var owner = e.target.parent.parent;
        if (owner.mamOznaceneObr){ 
            if (e.target.oznacene){
                e.target.oznacene = false;
                e.target.alpha = 1;
                owner.mamOznaceneObr = false;
                owner.oznacenyObr = null;
                console.debug("if");
            } else { //mam vybrate v obr ale klikla som na iny ako je vybraty
                owner.oznacenyObr.alpha = 1;
                owner.oznacenyObr.oznacene = false;
                e.target.oznacene = true;
                e.target.alpha = 0.3;
                owner.oznacenyObr = e.target;
                console.debug("obr "+ e.target.poradie);
                owner.overDvojicu(e.target.parent);
            }
        } else {//nemam oznacene nic v poli obrazkov
            e.target.oznacene = true;
            e.target.alpha = 0.3;
            owner.mamOznaceneObr = true;
            owner.oznacenyObr = e.target;
            console.debug("obr "+e.target.poradie);
            owner.overDvojicu(e.target.parent);
        }
    }
    this.addEventListener(MouseEvent.MOUSE_DOWN, this.down);
}
Obrazok.prototype = new Sprite (); 
           
function AtomyPolicka(x,y){
    Sprite.apply(this);
    this.x = x;
    this.y = y;
    this.atompolickaArray = Array (4);
                
    this.init = init;
    function init (){
        for(var i = 0; i < 4; i++){
            var array = new Array(5);  
            for (var j = 0; j < 5; j++){
                var obr = new Atom(i, j);
                array[j] = obr;
                //                            array[j].init();
                this.addChild(array[j]);
            }
            this.atompolickaArray[i] = array;
        }
    }
                    
    this.vloz = vloz;
    function vloz(att,i,j){  
        att.setIJ(i, j);
        att.oznacene = false;
        this.atompolickaArray[i][j] = att; 
    }
                
    this.miesanie = miesanie; 
    function miesanie(){
        for(var i = 0; i < 4; i++){  
            for (var j = 0; j < 5; j++){
                var randomI = Math.floor((Math.random()*4));
                var randomJ = Math.floor((Math.random()*5));
                var pom =  this.atompolickaArray[i][j];
                this.vloz(this.atompolickaArray[randomI][randomJ],i,j);
                this.vloz(pom,randomI,randomJ);
            }
        }
    }
}
AtomyPolicka.prototype = new Sprite();
           
Atom.prototype = new Sprite();
function Atom (i,j){
    Sprite.apply(this);
    this.poradie = i*5+j;
    this.oznacene = false;
    this.buttonMode = true;
                
    const IMAGE_URL= Game.picturesPath+"levels/levelElements/"+"at/"+this.poradie+".png";
    this.bitmap = Game._game.bitmaps[IMAGE_URL];
    this.bitmap.x = 0;
    this.bitmap.y = 0;
    this.bitmap.alpha = 1;        
    this.buttonMode = true;
    this.mouseChildren=false;
    this.addChild(this.bitmap); 
              
    this.setIJ = function(i,j){
        this.x = i*85;
        this.y = j*85+10;
        this.i = i;
        this.j = j;
    }
    this.setIJ(i,j);
               
    this.down = down;
    function down(e){
        var owner = e.target.parent.parent;
        if (owner.mamOznaceneAt){ 
            if (e.target.oznacene){
                e.target.oznacene = false;
                e.target.alpha = 1;
                owner.mamOznaceneAt = false;
                owner.oznacenyAt = null;
                console.debug("if");
            } else { //mam vybrate v obr ale klikla som na iny ako je vybraty
                owner.oznacenyAt.alpha = 1;
                owner.oznacenyAt.oznacene = false;
                e.target.oznacene = true;
                e.target.alpha = 0.3;
                owner.oznacenyAt = e.target;
                console.debug("at "+e.target.poradie);
                owner.overDvojicu(e.target.parent);
            }
        } else {//nemam oznacene nic v poli obrazkov
            e.target.oznacene = true;
            e.target.alpha = 0.3;
            owner.mamOznaceneAt = true;
            owner.oznacenyAt = e.target;
            console.debug("at "+e.target.poradie);
            owner.overDvojicu(e.target.parent);
        }
    }
    this.addEventListener(MouseEvent.MOUSE_DOWN, this.down);
}